<?php

class FriendDAO extends DAO {

	public static function getfriends($uid = 0, $db = 0)
	{
		$uid = intval($uid);
        if (!$uid) return false;
        
        $mck = "f:{$uid}";
        $ret = false;
        $mcconf = self::mchash("friend", $uid);
        $ret = self::getcache($uid, $mcconf, $mck, $db, __FUNCTION__);
        if ($ret)
		{
            if (!is_array($ret)) $ret = self::unzbin($ret);
            set_global("mc:{$mck}", $ret);
            return $ret;
        }
        $dbconf = self::dbhash($uid, "pvn_friend", "friend");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;
        
        $sql = "select * from {$dbconf[4]} where uid={$uid}";
        $res = self::dbquery($dbconn, $sql);
		$ret = array();
        if ($res && mysql_num_rows($res) >= 1)
		{
            while ($row = mysql_fetch_assoc($res))
            {
               
                $ret[$row["fuid"]] = $row;
            }
            unset($row);
        }
		elseif($res) $ret = array();
		else $ret = false;

		self::dbclose($dbconn);
		if($ret === false) return false;

		$bin = self::zbin($ret);
        if (self::setcache($uid, $mcconf, $mck, $bin, __FUNCTION__)) set_global("mc:{$mck}", $ret);

        return $ret;
	}

	public static function newfriends($uid = 0, $finfo = array())
	{
		$uid = intval($uid);
		if(!$uid || !$finfo || !is_array($finfo)) return false;

		if(self::getfriends($uid) === false) return false;

        $mcconf = self::mchash("friend", $uid);
        if (!self::delcache($uid, $mcconf, "f:{$uid}", __FUNCTION__)) return false;

		$dbconf = self::dbhash($uid, "pvn_friend", "friend");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;

		foreach($finfo as $fuid => $frow)
		{
			$fuid = intval($fuid);
			if(!$fuid) continue;

			$isnew = intval($frow['isnew']);
			$sql = "insert into {$dbconf[4]} (uid, fuid, isnew) values('{$uid}', '{$fuid}', '{$isnew}')";
			self::dbquery($dbconn, $sql);
		}
		self::dbclose($dbconn);

		return true;
	}

	public static function setfriend($uid = 0, $fuid = 0, $finfo = array())
	{
		$uid = intval($uid);
		$fuid = intval($fuid);
		if(!$uid || !$fuid) return false;

		$mcks = array();
		if(self::getfriends($uid) === false) return false;

        $mcconf = self::mchash("friend", $uid);
        if (!self::delcache($uid, $mcconf, "f:{$uid}", __FUNCTION__)) return false;

		$dbconf = self::dbhash($uid, "pvn_friend", "friend");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;

		$sets = array();
		foreach($finfo as $k => $v)
		{
			switch($k)
			{
				case 'isnew':
					$v = intval($v);
					$v = $v ? 1 : 0;
					$sets[] = "isnew='{$v}'";
					break;
			}
		}

		if(!$sets) return false;
		$sets = implode(",", $sets);
		$sql = "update {$dbconf[4]} set {$sets} where uid='{$uid}' and fuid='{$fuid}'";
		$ret = self::dbquery($dbconn, $sql);
		self::dbclose($dbconn);

		return $ret;
	}

	public static function delfriends($uid = 0, $fuids = array())
	{
		$uid = intval($uid);
		if(!$uid || !$fuids) return false;

		$fuids = implode(",", $fuids);
		if(!$fuids) return false;

		if(self::getfriends($uid) === false) return false;

		$mcconf = self::mchash("friend", $uid);
        if (!self::delcache($uid, $mcconf, "f:{$uid}", __FUNCTION__)) return false;

		$dbconf = self::dbhash($uid, "pvn_friend", "friend");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;

		$sql = "delete from {$dbconf[4]} where uid='{$uid}' and fuid in ({$fuids})";
		$ret = self::dbquery($dbconn, $sql);
		self::dbclose($dbconn);

		return $ret;
	}

	public static function clearfriends($uid = 0)
	{
		$uid = intval($uid);
		if(!$uid) return false;

		if(self::getfriends($uid) === false) return false;

		$mcconf = self::mchash("friend", $uid);
        if (!self::delcache($uid, $mcconf, "f:{$uid}", __FUNCTION__)) return false;

		$dbconf = self::dbhash($uid, "pvn_friend", "friend");
        $dbconn = self::getconn($uid, $dbconf, __FUNCTION__);
        if (!$dbconn) return false;

		$sql = "delete from {$dbconf[4]} where uid='{$uid}'";
		$ret = self::dbquery($dbconn, $sql);
		self::dbclose($dbconn);

		return $ret;
	}

}
